package com.caits.lbs.framework.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.caits.lbs.bean.dbmodel.ETBase;
import com.caits.lbs.framework.dao.IBusinessDAO;
import com.caits.lbs.framework.log.CommonLogFactory;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * <p>
 * BusinessDAOImpl
 * </p>
 * <p>
 * 用途：业务持久层的的实现基类
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-7-21
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-21 下午3:43:59</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-21 下午3:43:59</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class BusinessDAOImpl extends SqlMapClientDaoSupport implements
		IBusinessDAO {

	/** 日志记录器 */
	private Logger log = CommonLogFactory.getLog();
	
	public Long queryBusinessCount(String sqlId, Map<String, Object> paramMap)
			throws SQLException {
		Long total = (Long) getSqlMapClient().queryForObject(sqlId, paramMap);
		return total;
	}

	@SuppressWarnings("unchecked")
	public List<ETBase> queryBusinessList(String sqlId,
			Map<String, Object> paramMap) throws SQLException {
		List<ETBase> list = getSqlMapClient().queryForList(sqlId, paramMap);
		return list;
	}

	public Long queryBusinessCount(String sqlId, ETBase e) throws SQLException {
		Long total = (Long) getSqlMapClient().queryForObject(sqlId, e);
		return total;
	}

	@SuppressWarnings("unchecked")
	public List<ETBase> queryBusinessList(String sqlId, ETBase e)
			throws SQLException {
		List<ETBase> list = getSqlMapClient().queryForList(sqlId, e);
		return list;
	}

	public Map<String, Object> addBusiness(String sqlId,
			Map<String, Object> paramMap) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Long l = (Long) getSqlMapClientTemplate().insert(sqlId, paramMap);

			if ((l == null) || (l != null && l >= 1)) {
				map.put("result", l);
				map.put("msg", "添加成功!");
				map.put("id", l);
			} else {
				map.put("result", 0);
				map.put("msg", "添加失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	public Map<String, Object> addBusiness(String sqlId, ETBase e)
			throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Long l = (Long) getSqlMapClientTemplate().insert(sqlId, e);

			if ((l == null) || (l != null && l >= 1)) {
				map.put("result", l);
				map.put("msg", "添加成功!");
				map.put("id", l);
			} else {
				map.put("result", 0);
				map.put("msg", "添加失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	public Map<String, Object> deleteBusiness(String sqlId,
			Map<String, Object> paramMap) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			long l = getSqlMapClientTemplate().delete(sqlId, paramMap);
			if (l >= 1) {
				map.put("result", l);
				map.put("msg", "删除成功!");
				map.put("id", l);
			} else {
				map.put("result", 0);
				map.put("msg", "删除失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	public Map<String, Object> deleteBusiness(String sqlId, ETBase e)
			throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			long l = getSqlMapClientTemplate().delete(sqlId, e);
			if (l >= 1) {
				map.put("result", l);
				map.put("msg", "删除成功!");
				map.put("id", l);
			} else {
				map.put("result", 0);
				map.put("msg", "删除失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	public Map<String, Object> modifyBusiness(String sqlId,
			Map<String, Object> paramMap) throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			long l = getSqlMapClientTemplate().update(sqlId, paramMap);

			if (l >= 1) {
				map.put("result", l);
				map.put("msg", "修改成功!");
			} else {
				map.put("result", 0);
				map.put("msg", "修改失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	public Map<String, Object> modifyBusiness(String sqlId, ETBase e)
			throws SQLException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			long l = getSqlMapClientTemplate().update(sqlId, e);

			if (l >= 1) {
				map.put("result", l);
				map.put("msg", "修改成功!");
			} else {
				map.put("result", 0);
				map.put("msg", "修改失败!");
			}
		} catch (DataAccessException ex) {
			map.put("result", 0);
			map.put("msg", "添加失败!");
			map.put("id", -1);
			log.error("数据库访问异常sqlId="+sqlId+ex.getLocalizedMessage());
		}
		return map;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.caits.lbs.framework.dao.IBusinessDAO#queryList(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> queryList(String sqlId) throws SQLException {
		return getSqlMapClient().queryForList(sqlId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.caits.lbs.framework.dao.IBusinessDAO#queryBusiness(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public ETBase queryBusiness(String sqlId, Map<String, Object> paramMap)
			throws SQLException {
		return (ETBase) getSqlMapClient().queryForObject(sqlId, paramMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.caits.lbs.framework.dao.IBusinessDAO#queryList(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public <T> List<T> queryList(String sqlId, Map<String, Object> paramMap)
			throws SQLException {
		return getSqlMapClient().queryForList(sqlId, paramMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.caits.lbs.framework.dao.IBusinessDAO#queryList(java.lang.String,
	 * com.caits.lbs.bean.dbmodel.ETBase)
	 */
	@Override
	public <T> List<T> queryList(String sqlId, ETBase e) throws SQLException {
		return getSqlMapClient().queryForList(sqlId, e);
	}
	
	/* (non-Javadoc)
	 * @see com.caits.lbs.framework.dao.IBusinessDAO#addBussinessList(java.lang.String, java.util.List)
	 */
	public Map<String, Object> addBussinessList(String sqlId,List<Map<String,Object>> list) throws SQLException {
		Map<String, Object> retMap = new HashMap<String, Object>();
		SqlMapClient  executor =getSqlMapClient();
		try{
			executor.startBatch();
	        for (int i = 0; i < list.size(); i++) {
	        	Map<String,Object> mapParam = list.get(i);
	            Object obj = executor.update(sqlId, mapParam);
	            retMap.put(""+i, obj);
	        }
	        executor.executeBatch();
		}catch(Exception e){
			log.error("执行批量插入异常,sqlId"+sqlId, e);
			throw new SQLException("执行批量插入异常,msg="+e.getLocalizedMessage(),e);
		}
		return retMap;
		
	}
}
